package b1.mobile.blackberry.ui.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;

public class SAPTitleBar extends Field {
	private String text;
	private Bitmap bitmap;
	private Font font;

	public SAPTitleBar(String text) {
		super(USE_ALL_WIDTH);
		this.text = text;
		this.font = Font.getDefault();
		try {
			FontFamily ff = FontFamily.forName("Alpha Sans");
			font = ff.getFont(Font.BOLD, 7, Ui.UNITS_pt);
		} catch (ClassNotFoundException e) {
			
		}
	}

	public SAPTitleBar(String text, Bitmap bitmap, Font font) {
		super(USE_ALL_WIDTH);
		this.text = text;
		this.bitmap = bitmap;
		this.font = font;
	}

	protected void layout(int width, int height) {

		if (bitmap != null) {
			setExtent(width, font.getHeight()*15/10);
		} else {
			setExtent(width, font.getHeight()*15/10);
		}
	}

	protected void paint(Graphics graphics) {
		int rowHeight = 32;
		Bitmap icon = null;
		int imageOffset = 0;
		graphics.setFont(font);
		
		if (bitmap != null) {
			rowHeight = this.bitmap.getHeight();
			icon = this.bitmap;
			int imageHeight = icon.getHeight();
			imageOffset = (rowHeight - imageHeight) / 2;
		}

		int fontHeight = font.getHeight();

		int fontOffset = (rowHeight - fontHeight) / 2;

		int[] colors = new int[] { Color.GRAY, Color.BLACK, Color.BLACK, Color.BLACK };

		int[] yPoints = new int[] { 0, 0, rowHeight, rowHeight };
		int[] xPoints = new int[] { 0, Display.getWidth(), Display.getWidth(), 0 };

		graphics.drawShadedFilledPath(xPoints, yPoints, null, colors, null);

		if (bitmap != null) {
			graphics.drawBitmap(imageOffset, imageOffset, icon.getWidth(), icon.getHeight(), icon,
					0, 0);
			graphics.setColor(0xf3a93a);
			
			graphics.drawText(this.text, fontOffset + icon.getWidth() + fontOffset, fontOffset,
					Graphics.ELLIPSIS, Display.getWidth()
							- (imageOffset + icon.getWidth() + imageOffset));
		} else {
			graphics.setColor(0xf3a93a);
			graphics.drawText(text, (getWidth()-font.getAdvance(text))/2, (getHeight()-font.getHeight())/2);
		}
		
		
	}
}
